package com.yd.controller.sys;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yd.pojo.Sys;
import com.yd.service.SysService;
import com.yd.tool.MD5Hash;
import com.yd.tool.UtsReg;
import com.yd.tool.UtsUuid;

@Controller
public class SysControllerSys {

	@Resource
	private SysService sysService;

	@RequestMapping(value = "/sys/sys/token")
	@ResponseBody
	public String sys_sys_token(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		String token="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		sys.setLoginpass("");
		jsonObject.put("data", sys);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
		
	}
	
	@RequestMapping(value = "/sys/sys/login")
	@ResponseBody
	public String sys_sys_login(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		//获取参数
		String loginname="";
		String loginpass="";
		//验证参数
		if (UtsReg.getStringKB(request.getParameter("loginname"))){loginname = request.getParameter("loginname").trim();}else{jsonObject.put("msg", "账号错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getStringPass(request.getParameter("loginpass"))){loginpass = request.getParameter("loginpass").trim();}else{jsonObject.put("msg", "密码错误");jsonObject.put("error", "1");return jsonObject.toString();}
		//通过账号查询此条数据
		//Users users=usersService.selectOne(new EntityWrapper<Users>().eq("loginname", loginname));
		Sys sys = sysService.selectOne(new EntityWrapper<Sys>().eq("loginname", loginname));
		//判断此账号是否有数据
		if(sys==null){//如果没有数据 那说明不存在此账号
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//走到这里说明有账号，那么要判断这个账号的密码是否正确，传过来的密码要加密后再对比
		if(!sys.getLoginpass().equals(MD5Hash.getMd5HashPasswod(loginpass))){
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//生成一个随机数当作token
		sys.setToken(UtsUuid.getUuid());
		//把这个token更新到数据库，每次登陆都要重新生成一次
		if(!sysService.updateById(sys)){
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//把实体类里面的密码清空 然后返回给前台(所有返回用户信息的时候都要清空重要的信息，比如密码，身份证，等等)
		sys.setLoginpass("");
		jsonObject.put("data", sys);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
		
		
		//List<Users> users = usersService.selectList(new EntityWrapper<Users>().allEq(params))
		
	}
	
	@RequestMapping("/sys/sys/pass/up")
	@ResponseBody
	public String sys_sys_pass_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String ypass = "";
		String npass = "";
		String npass2 = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringPass(request.getParameter("ypass"))){ypass=request.getParameter("ypass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "原密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("npass"))){npass=request.getParameter("npass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "新密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("npass2"))){npass2=request.getParameter("npass2").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "重复密码 格式错误");return jsonObject.toString();}
		
		if(!npass.equals(npass2)){jsonObject.put("error", "1");jsonObject.put("msg", "两次密码不一致");return jsonObject.toString();}
		
		Sys sys = sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "请重新登陆");return jsonObject.toString();
		}
		
		if(!MD5Hash.getMd5HashPasswod(ypass).equals(sys.getLoginpass())){
			jsonObject.put("error", "1");jsonObject.put("msg", "原始密码 错误");return jsonObject.toString();
		}
		
		sys.setLoginpass(MD5Hash.getMd5HashPasswod(npass2));
		
		if(!sysService.updateById(sys)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();

	}
	
	@RequestMapping("/sys/sys/touxiang/up")
	@ResponseBody
	public String sys_sys_touxiang_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String img = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringTitle(request.getParameter("img"))){img = request.getParameter("img").trim();}else{jsonObject.put("msg", "图片 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Sys sys =sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		sys.setImg(img);
		if(!sysService.updateById(sys)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	@RequestMapping("/sys/sys/nicheng/up")
	@ResponseBody
	public String sys_sys_nicheng_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String nicheng = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "token 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("nicheng"))){nicheng = request.getParameter("nicheng").trim();}else{jsonObject.put("msg", "昵称格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}

		Sys sys =sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("nicheng", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		 
		sys.setNicheng(nicheng);
		if(!sysService.updateById(sys)){
			jsonObject.put("nicheng", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	@RequestMapping("/sys/sys/name/up")
	@ResponseBody
	public String sys_sys_name_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String name = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("name"))){name=request.getParameter("name").trim();}else{jsonObject.put("msg", "名称  错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Sys sys =sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("name", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		sys.setName(name);
		if(!sysService.updateById(sys)){
			jsonObject.put("name", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	@RequestMapping("/sys/sys/email/up")
	@ResponseBody
	public String sys_sys_email_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String email = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringEmail(request.getParameter("email"))){email = request.getParameter("email").trim();}else{jsonObject.put("msg", "昵称格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Sys sys =sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("email", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		sys.setEmail(email);
		if(!sysService.updateById(sys)){
			jsonObject.put("email", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	
	
}
